# Definition for singly-linked list.
class ListNode(object):
    def __init__(self, x):
        self.val = x
        self.next = None

        '''
        所以我们首先要统计链表中结点的总个数，然后除以k，得到的商avg就是能分成的部分个数，
        余数ext就是包含有多余的结点的子链表的个数。
        '''

class Solution(object):
    def splitListToParts(self, root, k):
        """
        :type root: ListNode
        :type k: int
        :rtype: List[ListNode]
        """
        if not root:
            return [[] for i in range(k)]
        vals = []
        while root:
            vals.append(root.val)
            root = root.next
        d, m = divmod(len(vals), k)
        res = [vals[d * (i - 1) + i - 1: d * i + i] for i in range(1, m + 1)]
        res.extend([vals[d * m + m + i * d: d * m + m + (i + 1) * d] for i in range(k - m)])
        return res



